package com.ruoyi.business.service.koc.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.dto.admin.user.TbUserBabyPageQueryDTO;
import com.ruoyi.business.domain.dto.api.user.AddMyBabyDTO;
import com.ruoyi.business.domain.dto.api.user.EditMyBabyDTO;
import com.ruoyi.business.domain.koc.TbUserBaby;
import com.ruoyi.business.domain.vo.TbUserBabyPageVO;
import com.ruoyi.business.mapper.koc.TbUserBabyMapper;
import com.ruoyi.business.service.koc.ITbUserBabyService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.business.BabyStatusEnum;
import com.ruoyi.common.enums.business.GenderEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;

@RequiredArgsConstructor
@Service
public class TbUserBabyServiceImpl extends ServiceImpl<TbUserBabyMapper, TbUserBaby> implements ITbUserBabyService {

    @Override
    public TableDataInfo<TbUserBaby> queryMyBabyPageList(Long userId, PageQuery pageQuery) {
        LambdaQueryWrapper<TbUserBaby> qwl = Wrappers.lambdaQuery(TbUserBaby.class)
            .eq(TbUserBaby::getUserId, userId).orderByDesc(TbUserBaby::getCreateTime);
        Page<TbUserBaby> page = this.page(pageQuery.build(), qwl);
        return TableDataInfo.build(page);
    }

    @Override
    public boolean editMyBaby(Long userId, EditMyBabyDTO editMyBabyDTO) {
        TbUserBaby tbUserBaby = this.getById(editMyBabyDTO.getId());
        tbUserBaby.setNickname(StrUtil.isNotEmpty(editMyBabyDTO.getNickname()) ? editMyBabyDTO.getNickname() : StrUtil.EMPTY);
        tbUserBaby.setBirthday(editMyBabyDTO.getBirthday());
        tbUserBaby.setGender(ObjectUtil.isNotEmpty(editMyBabyDTO.getGender()) ? editMyBabyDTO.getGender() : GenderEnum.UNKNOWN.getCode());
        tbUserBaby.setStatus(ObjectUtil.isNotEmpty(editMyBabyDTO.getStatus()) ? editMyBabyDTO.getStatus() : BabyStatusEnum.BORN.getCode());
        tbUserBaby.setRemarks(StrUtil.isNotEmpty(editMyBabyDTO.getRemarks()) ? editMyBabyDTO.getRemarks() : StrUtil.EMPTY);
        tbUserBaby.setUpdateTime(new Date());
        return this.updateById(tbUserBaby);
    }

    @Override
    public boolean addMyBaby(Long userId, AddMyBabyDTO addMyBabyDTO) {
        TbUserBaby userBaby = new TbUserBaby();
        userBaby.setId(IdWorker.getId());
        userBaby.setUserId(userId);
        userBaby.setNickname(StrUtil.isNotEmpty(addMyBabyDTO.getNickname()) ? addMyBabyDTO.getNickname() : StrUtil.EMPTY);
        userBaby.setBirthday(addMyBabyDTO.getBirthday());
        userBaby.setGender(ObjectUtil.isNotEmpty(addMyBabyDTO.getGender()) ? addMyBabyDTO.getGender() : GenderEnum.UNKNOWN.getCode());
        userBaby.setStatus(ObjectUtil.isNotEmpty(addMyBabyDTO.getStatus()) ? addMyBabyDTO.getStatus() : BabyStatusEnum.BORN.getCode());
        userBaby.setRemarks(StrUtil.isNotEmpty(addMyBabyDTO.getRemarks()) ? addMyBabyDTO.getRemarks() : StrUtil.EMPTY);
        userBaby.setCreateTime(new Date());
        return this.save(userBaby);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteByIds(Collection<String> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public TableDataInfo<TbUserBabyPageVO> queryPageList(TbUserBabyPageQueryDTO dto, PageQuery pageQuery) {
        Page<TbUserBabyPageVO> page = pageQuery.build();
        IPage<TbUserBabyPageVO> pageRes = this.getBaseMapper().selectPageList(page, dto);
        return TableDataInfo.build(pageRes);
    }
}
